package com.gmail.hongyz.myfuse.dataengine;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;

import com.gmail.hongyz.myfuse.common.PageObject;

public class Dataset {

	private List<Record> records = new ArrayList<Record>();
	private PageObject pageObject;

	public Dataset() {
	}
	public int size() {
		return records.size();
	}
	public void addRecord(Record record) {
		records.add(record);
	}
	public Record getRecord(int index) {
		return records.get(index);
	}
	
	public static Dataset convertToDataset(List list) {
		if (list == null) return null;
		Dataset dataset = new Dataset();
		for (Object object : list) {
			Record record;
			try {
				record = new Record(PropertyUtils.describe(object));
				dataset.addRecord(record);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return dataset;
	}
	public PageObject getPageObject() {
		return pageObject;
	}
	public void setPageObject(PageObject pageObject) {
		this.pageObject = pageObject;
	}
	
	@Override
	public String toString() {
		return this.getClass() + ": [pageObject:" + pageObject + ", records: " + records + "]"; 
	}
}
